<?php
include("read.php");
include("../lib/utilities.php");
$db = openDB("../database.sqlite");
$config = getConfig();

/******* page edits **********/

if(!$_POST['edit_body'] == "") {
  // get latest id's
  $latestid = getLatest($db);
  // get requested title
  $name = $_GET['name'];
  // remember author
  setcookie("name",$_POST['edit_author'],time()+60*60*24*30);
  // get links
  $links = array();
  $split = split("}",$_POST['edit_body']);
  for($i=0; $i<count($split)-1; $i++) {
    $segment = $split[$i];
    $split2 = split("{",$segment);
    $linktarget = $split2[1];
    array_push($links,$linktarget);
  }
  // check if page already exists
  if(!exists("pages","name",$name)) {
    // NEW PAGE
    // add to pages table
    $id = $latestid['pages']+1;
    // add to pages table
    $query = "INSERT INTO pages (
      id,
      name,
      parent
    ) VALUES (
      $id,
      '$name',
      '".sqlite_escape_string(stripslashes($_POST['edit_parent']))."'
    )";
    sqlite_query($db,$query);
  } else {
    // EDIT/REVISION
    // find the page's id
    $id = sqlite_fetch_single(sqlite_query($db,"SELECT id FROM pages WHERE name='$name'"));
    // update parent
    sqlite_query($db,"UPDATE pages SET parent='".$_POST['edit_parent']."' WHERE name LIKE '$name'");
  }
  // write the new revision
  sqlite_query($db,"INSERT INTO revisions (
    author,
    ip,
    pageid,
    time,
    body
  ) VALUES (
    '".$_POST['edit_author']."',
    '".$_SERVER['REMOTE_ADDR']."',
    $id,
    ".time().",
    '".sqlite_escape_string(stripslashes($_POST['edit_body']))."'
  )");
  // delete existing links from this page
  sqlite_query($db,"DELETE FROM links WHERE fromid = $id");
  // write links
  foreach($links as $link) {
    sqlite_query($db,"INSERT INTO links (
      fromid,
      to
    ) VALUES (
      $id,
      '$link'
    )");
  }
  // go back to the page
  header("location: ".getURL('show',$name));
} else if(!$_POST['comment_body'] == "") {
  
  /****** comments *******/
  
  $query = "INSERT INTO comments (
    pageid,
    time,
    name,
    ip,
    body
  ) VALUES (
    ".getIdFromName($_GET['name']).",
    ".time().",
    '".sqlite_escape_string(stripslashes($_POST['comment_name']))."',
    '".$_SERVER['REMOTE_ADDR']."',
    '".sqlite_escape_string(stripslashes($_POST['comment_body']))."'
  )";
  sqlite_query($db,$query);
  
  setcookie("name",$_POST['comment_name'],time()+60*60*24*30);
  
  header("location: ".getURL('discuss',$_GET['name'])."#".time());
  
} else if(!$_POST['config_name'] == "") {

  /***** config *******/
  
  if(!$_POST['config_url'] == "") {
    sqlite_query($db,"UPDATE config SET url = '".sqlite_escape_string(stripslashes($_POST['config_url']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_name'] == "") {
    sqlite_query($db,"UPDATE config SET name = '".sqlite_escape_string(stripslashes($_POST['config_name']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_tagline'] == "") {
    sqlite_query($db,"UPDATE config SET tagline = '".sqlite_escape_string(stripslashes($_POST['config_tagline']))."' WHERE url LIKE 'http://%'");
  }
  if(!$_POST['config_skin'] == "") {
    sqlite_query($db,"UPDATE config SET skin = '".sqlite_escape_string(stripslashes($_POST['config_skin']))."' WHERE url LIKE 'http://%'");
  }
  if($_POST['config_mod_rewrite'] == "on") {
    sqlite_query($db,"UPDATE config SET mod_rewrite_enabled = 'true'");
  } else {
    sqlite_query($db,"UPDATE config SET mod_rewrite_enabled = 'false'");
  }
  header("location: ".$_POST['config_url']);
}
?>